package com.wondertek.poms.sync.manager.main;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.core.service.IPrdContService;
import com.wondertek.poms.core.service.IPrdInfoService;
import com.wondertek.poms.core.utils.OutHttpUtil;
import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.PrdCont;
import com.wondertek.poms.dao.po.PrdInfo;
import com.wondertek.poms.dao.po.SyncTask;
import com.wondertek.poms.sync.manager.copyright.CopyrightManager;

import lombok.extern.slf4j.Slf4j;

/**
 * <p><b>MainAssertManager Description:</b> 其他类型媒资业务类</p>
 * @author douzi
 * <b>DATE</b> 2019年8月20日 下午5:28:39
 */
@Slf4j
@Component
public class NoProductMainAssertManager extends ParentManager {
	
    @Autowired
    IPrdInfoService prdInfoService;
    @Autowired
    IPrdContService prdContService;
    @Autowired
    CopyrightManager copyrightManager;
    @Autowired
    OutHttpUtil outHttpUtil;

    /**
     * <p><b>Title:</b> mainProcess</p>
     * <p><b>Description:</b> 无二级产品主业务流程</p>
     *
     * @param vo
     * @author douzi
     */
    public void mainProcess(Content c, SyncTask st, List<PrdInfo> normalList) {
        switch (c.getStatus().intValue()) {
            case Constants.ASSET_STATUS_0:
            case Constants.ASSET_STATUS_1:
            case Constants.ASSET_STATUS_3:
                mainUpdateProcess(c, st, normalList);
                break;
            case Constants.ASSET_STATUS_2:
                mainDelProcess(st, "nomal");
                break;
            default:
                log.info("普通包主流程|媒资:" + st.getAssetId() + "|不属于任何一种媒资状态:" + c.getStatus());
                break;
        }
    }

    /**
     * <p><b>Title:</b> mainUpdateProcess</p>
     * <p><b>Description:</b> </p>
     * @author douzi
     * @param c
     * @param st
     * @param normalList
     */
    private void mainUpdateProcess(Content c, SyncTask st, List<PrdInfo> normalList) {
    	normalList.stream().parallel().forEach(pi -> {
//    	normalList.stream().forEach(pi -> {
    		boolean isContinue = filterBlack(c, pi);
    		if (isContinue) {
    			log.info("普通包主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + 
    					"|无二级产品的商品包：" + pi.getPrdInfoId() + "属于黑名单");
    			return;
    		}
    		
    		Long newPrdInfoId = filterWhite(c, pi);
    		if (newPrdInfoId == null) {
    			newPrdInfoId = getPrdInfoRules(c, pi);
    		}
    		
    		List<PrdCont> prdContDbs = prdContService.getPrdContByMmsIdInPrdInfo(c.getAssetId(), pi.getPrdInfoId());
            log.info("普通包主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() +  "|无二级产品的商品包|能进入的包id:"
                    + newPrdInfoId + "|数据库中媒资数量:" + prdContDbs.size() + "|版权id:" + c.getCopyrightObjectId());

            if (newPrdInfoId == null) {
            	if (prdContDbs !=null && prdContDbs.size() > 0) {
            		PrdCont pcDb = prdContDbs.get(0);
            		//只有相同内容时，需要下线；其他内容不能下线
            		if (pcDb.getContentId().equals(c.getContentId())) {
            			offineContent(prdContDbs.get(0), pi, c, "normal");
            		}
            	}
                return;
            }
            
    		PrdInfo prdInfo = getPrdInfo(normalList, newPrdInfoId);
    		if (prdContDbs.size() <= 0) {
                inPrdInfo(prdInfo, c, null);
            } else if (prdContDbs.size() == 1) {
            	PrdCont pcDb = prdContDbs.get(0);
            	// 比较数据库中的内容是否和传入内容相同，不同需要走版权pk业务
            	if (pcDb.getContentId().equals(c.getContentId())) {
                    inPrdInfo(prdInfo, c, pcDb);
            	} else {
            		findOptimalPrdCont(c, prdInfo, pcDb, "普通包");
            	}
            }

            //如果是壳，需要让子集跟着一起去新包
            subSetToNewPrdInfo(newPrdInfoId, prdContDbs, c);
    	});
	}
}
